package com.boot.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.boot.entity.mongo.BaseData;
import com.boot.entity.Girl;
import com.boot.mapper.GirlMapper;
import com.boot.service.GirlService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created  on 2018/7/5 10:51.
 * 作者: Dengjb
 * 说明:
 */
@RestController
public  class GirlController {
	@Autowired
	private GirlService girlService;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	GirlMapper girlMapper;
	@Autowired
	private MongoTemplate mongoTemplate;
		@RequestMapping("/findByName")
	    public Object findByName(@Param("name") String name){
			return null;
		}
	@RequestMapping("/insertGirl")
	public void insertGirl(Girl girl){
//		 girlService.insert(girl);
	}
	@RequestMapping("/getClasses")
	public Object getClasses(){
		return null;
	}
	@RequestMapping("/getStudents")
	public Object getStudents(){
		return null;
	}
	@RequestMapping("/redis")
	public Object redis(){
			Girl girl = new Girl();
			girl.setName("json");
			girl.setAge(20);
		//		String s = JSON.toJSONString(girl);
		//		stringRedisTemplate.opsForValue().set("girl1",s);
		//		String girl1 = stringRedisTemplate.opsForValue().get("girl");
//		List<Girl> girls = new ArrayList<>();
//		girls.add(girl);
//		girls.add(girl);
//		stringRedisTemplate.opsForValue().set("girls",JSON.toJSONString(girls));

		String girlsList = stringRedisTemplate.opsForValue().get("girls");
		List<Girl> girls1 = JSON.parseArray(girlsList, Girl.class);
		return girls1;
	}
	@RequestMapping("/mongo")
	public Object mongo(){
//		Girl girl = new Girl();
////		girl.setId(5);
////		girl.setAge("18");
////		girl.setName("green1");
////		mongoTemplate.save(girl);
//		List<Girl> girls = girlService.selectByMap(null);
//		for (Girl girl :girls){
//			mongoTemplate.insert(girl);
//		}
//		Update update = Update.update("name", "mongo");
//		mongoTemplate.updateFirst(query, update, Girl.class);

//		Girl one = mongoTemplate.findOne(query, Girl.class);
//		one.getAge();
//		Map one = mongoTemplate.findOne(query, Map.class);
//		BaseData da = new BaseData();
//		da.setAuth(1);
//		mongoTemplate.insert(da);
		Query query = new Query();
//		query.addCriteria(Criteria.where("utime").gte("2017-07-11"));
//		query.addCriteria(Criteria.where("md5").is("db749d634666d873209854524122ab2b"));
		query.addCriteria(Criteria.where("appData.chapter").is("ed89ccd5-04f6-4b42-b6be-c179f79eb275"));

//		Update update = Update.update("speaker", "Mr.wang");
//		Query queryUpdate = new Query();
//		queryUpdate.addCriteria(Criteria.where("md5").is("db749d634666d873209854524122ab2b"));
//		mongoTemplate.updateFirst(queryUpdate,update,BaseData.class);
		BaseData one = mongoTemplate.findOne(query,BaseData.class);
		return  one;
	}
	@RequestMapping("/selectMapper")
	public Object selectMapper(){
//		return  girlMapper.selectByPrimaryKey(1);
		return girlService.selectList(null);
	}
	@RequestMapping("/list")
	public Object list(){
//		return  girlMapper.selectByPrimaryKey(1);
		Page page = new Page();
		page.setSize(5);
		page.setCurrent(1);
		page.setOrderByField("age");
		return girlService.selectGirlList(page);
	}
}
